<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use App\Services\Tools\RedisService;
use Illuminate\Support\Facades\Hash;    //引入hash

class UserDataRepository {
    /**
     * 获取用户id
     * @param $where
     * @return bool
     * @throws BasicException
     */
    public function getUserId($where = []) {
        $user_id = app('model_user_data')->where($where)->value('user_id');
        if ($user_id) return $user_id;
        throw new BasicException(93046);
    }

    /**
     * 获取满足条件的所有用户id
     * @param $keyword
     * @return bool
     */

    public function getAllUserIds($keyword){
        $user_ids = app('model_user_data')
            ->where('user_code','like',"%{$keyword}%")
            ->orWhere('nickname','like',"%{$keyword}%")
            ->select('user_id','user_code','nickname')
            ->get()->toArray();
        return $user_ids;
    }

    /**
     * 查看用户编号是否已经存在
     * @param $code
     * @return bool
     */
    public function isExistUserCode($code): bool
    {
        return app('model_user_data')->where('user_code', $code)->exists() || app('model_user_data')->where('last_user_code', $code)->exists();
    }

    /**
     * 获取用户信息
     * @param $userCode
     * @return mixed
     */
    public function getUserInfoByUserCode($userCode)
    {
        return app('model_user_data')
            ->where(function ($query) use ($userCode) {
                $query->where('user_code',$userCode)->orWhere('last_user_code',$userCode);
            })
            ->withOnly('good_number',['user_id','good_number'])
            ->select('user_id','user_code','nickname','head_picture')
            ->first()->toArray();
    }

    /**
     * 重置用户编号
     * @param $userId
     * @return mixed
     * @throws BasicException
     */
    public function resetUserCode($userId)
    {
        $userData = app('model_user_data')->where('user_id', $userId)->first();
        $userData->user_code = $userData->last_user_code;
        $res = $userData->save();
        if ($res) return $res;
        throw new BasicException(97008);
    }

    /**
     * 靓号赋值用户编号
     * @param int $userId
     * @param int $goodNumber
     * @return mixed
     * @throws BasicException
     */
    public function goodNumber2UserCode(int $userId, int $goodNumber)
    {
        $userData = app('model_user_data')->where('user_id', $userId)->first();
        $userData->last_user_code = $userData->user_code;
        $userData->user_code = $goodNumber;
        $res = $userData->save();
        if ($res) return $res;
        throw new BasicException(97010);
    }

    /**
     * 获取用户信息
     * @param  $user_id
     * @return mixed
     * @throws BasicException
     */
    public function  getUserInfo($user_id){
        $user_info = app('model_user_data')->where('user_id',$user_id)->select('nickname','user_code')->first();
        if ($user_info) return $user_info;
        throw new BasicException(93046);
    }


    /**
     * 获取用户id
     * @param  $param
     * @return mixed
     */
    public   function  getUserIds($param){
        $user_ids = app('model_user_data')->whereIn('user_code',$param)->select('user_id')->get()->toArray();
        return $user_ids;
    }


    public function getBaseUserInfo($user_id)
    {
        $result = app('model_user_data')->with('user:ry_token,user_id,role,register_time,rank_id,nobility,channel_stage_id')->select([
            'user_code', 'sex', 'head_picture', 'nickname', 'user_id', 'new_love_id'
        ])->where('user_id', $user_id)->first();
        if (!$result) {
            return [];
        }
        $result = $result->toArray();
        $user_data = $result['user'] ?? [];
        unset($result['user']);
        $user_data = array_merge($result, $user_data);

        $rank_id = $user_data['rank_id'] ?? 0;
        $nobility = $user_data['nobility'] ?? 0;
        $user_data['register_time'] = $user_data['register_time'] ?? 0;
        $user_data['rank_icon'] = app('model_grade_config')->getGradeIcon($rank_id);
        $nobility_info = app('model_nobility')->getNobilityInfo($nobility);
        $user_data['nobility_icon'] = $nobility_info['head_picture'] ?? '';
        $user_data['moods'] = $nobility_info['moods'] ?? 10;
        $user_data['nobility_name'] = $nobility_info['name'] ?? '';
        $user_data['user_is_new'] = app('model_user')->userIsNew($user_data['register_time']);
        $user_data['dress_picture'] = RedisService::getHeadPicture($user_id);//头像框
        //用户新的性别和恋爱类型v1.2.0
        $user_data = $this->getUserSexAndLove($user_data);
        return $user_data;
    }

    /**
     * @Desc:不使用缓存获取用户性别和恋爱类型,性别类型先不做
     * @param $info
     * @return mixed
     * @author: shiquan
     * @Time: 2022/11/11 11:06
     */
    public function getUserSexAndLove($info)
    {
        /*if (isset($info['new_sex_id']) && $info['new_sex_id'] > 0){
            $sexInfo = app('model_user_sex_and_love')->where('id', $info['new_sex_id'])->first();
            $info['new_sex_title'] = $sexInfo['title'] ?? '';
            $info['new_sex_icon'] = $sexInfo['image'] ?? '';
        } else {
            $info['new_sex_title'] = '';
            $info['new_sex_icon'] = '';

        }*/
        //用户新的恋爱类型
        if (isset($info['new_love_id']) && $info['new_love_id'] > 0){
            $loveInfo = app('model_user_sex_and_love')->where('id', $info['new_love_id'])->first();
            $info['new_love_title'] = $loveInfo['title'] ?? '';
            $info['new_love_icon'] = $loveInfo['image'] ?? '';
        } else {
            $info['new_love_title'] = '';
            $info['new_love_icon'] = '';
        }

        return $info;
    }
}


?>